<?php

namespace Beerus\Modules\Wiki\Controllers;

use App\Http\Controllers\Controller;
use Doraemon\model\Category as mCategory;
use Biz\service\brand\Brand;
use Biz\service\Tag as sTag;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Doraemon\service\Spu as sSpu;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;

class IndexController extends Controller
{

	const PAGE_SIZE = 50;

	public function index(Request $request)
	{
		$page = $request->input('page', 1);
		$wikiName = $request->input('wikiName', '');
		$brandName = $request->input('brandName', '');

		list($list, $count) = sSpu::getSelfBuildWikiList($brandName, $wikiName, $page, self::PAGE_SIZE);
		$page = $this->paginator($list, $count, [
			'wikiName' => $wikiName,
			'brandName' => $brandName
		], self::PAGE_SIZE);

		$view = view('wiki::index.index', [
				'list' => $list,
				'count' => $count,
				'wikiName' => $wikiName,
				'brandName' => $brandName,
				'page' => $page
			]
		);
		return Admin::content(function (Content $content) use ($view) {
			$content->header('wiki审核与合并');
			$content->body($view);
		});
	}

	public function mergePage(Request $request)
	{

		$wikiIds = $request->input('wiki_ids', []);
		$wikiName = $request->input('wikiName', '');
		$brandName = $request->input('brandName', '');
		$infos = sSpu::getSpuListByIDs4App($wikiIds);

		$view = view('wiki::index.mergepage', [
				'infos' => $infos,
				'brandName' => $brandName,
				'wikiName' => $wikiName,
			]
		);
		return Admin::content(function (Content $content) use ($view) {
			$content->header('wiki合并');
			$content->body($view);
		});

	}

	public function searchWiki(Request $request)
	{
		$key = $request->input('key', '');
		$data['code'] = 0;
		$data['data'] = [];
		if ($key) {

			list($list, $count) = sSpu::searchWikiByKey($key);

			$data['code'] = 1;
			$data['data'] = ['count' => $count, 'list' => $list];

		}

		return json_encode($data, JSON_UNESCAPED_UNICODE);

	}

	public function replaceWiki(Request $request)
	{
		$wiki_ids = $request->input('wiki_ids', []);
		$replace_wiki_id= $request->input('replace_wiki_id', '');

		$wiki_ids = array_diff($wiki_ids, [$replace_wiki_id]);
		if ($wiki_ids) foreach ($wiki_ids as $wiki_id)
			if ($wiki_id && $replace_wiki_id) {

				sSpu::replaceWiki($wiki_id, $replace_wiki_id, Admin::user()->id);

			}

		return $this->index($request);

	}

	public function editWiki(Request $request)
	{
		$wiki_id= $request->input('wiki_id', '');
		$is_edit = $request->input('is_edit', '');

		if ($is_edit) {

			$data = [
				'cover_img' => $request->input('cover_img', ''),
				'title' => $request->input('title', ''),
				'min_price' => $request->input('min_price', 0),
				'max_price' => $request->input('max_price', 0),
				'series' => $request->input('series', ''),
				'brand_id' => $request->input('brand_id', 0),
				'last_category_id' => $request->input('last_category_id', 0),
				'imgs' => $request->input('imgs', []),
				'tags' => $request->input('tags', '')
			];

			$rowCount = sSpu::editCustomWiki($wiki_id, $data);

//			var_dump($rowCount);die;
		}

		if ($wiki_id) {

			$wikiInfo = sSpu::getWikiInfo([$wiki_id]);
		}
		$lastStageCates = mCategory::selectLastStageCate();

		$view = view('wiki::index.wikiedit', [
				'wikiInfo' => $wikiInfo[0],
				'lastStageCates' => $lastStageCates
			]
		);
		return Admin::content(function (Content $content) use ($view) {
			$content->header('wiki编辑');
			$content->body($view);
		});
	}

	public function searchBrand(Request $request)
	{
		$key = $request->input('key');

		$returnRst = [];

		if ($key) {
			$brands = Brand::mgtGetSuggestByBrandName($key);

			$returnRst = array_map(function ($brand){
				return ["id" => $brand['id'], "text" => $brand['brand_name']];
			}, $brands);
		}

		return json_encode($returnRst, JSON_UNESCAPED_UNICODE);
	}

	public function searchStyle(Request $request)
	{
		$key = $request->input('key');
		$returnRst = [];
		if ($key) {
			$styles = sTag::mgtSuggestStyle($key);

			$returnRst = array_map(function ($style) {
				return ["id" => $style['tag'], "text" => $style['tag']];
			}, $styles);

		}

		return json_encode($returnRst, JSON_UNESCAPED_UNICODE);

	}

}